{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算样本量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "#引用包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import pymysql\n",
    "import warnings\n",
    "import statsmodels.stats.weightstats as sw\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-01 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-01 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-01 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-01 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-01 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  user_id                  timestamp      group landing_page  converted\n",
       "0  851104 2017-01-01 22:11:48.556739    control     old_page          0\n",
       "1  804228 2017-01-01 08:01:45.159739    control     old_page          0\n",
       "2  661590 2017-01-01 16:55:06.154213  treatment     new_page          0\n",
       "3  853541 2017-01-01 18:28:03.143765  treatment     new_page          0\n",
       "4  864975 2017-01-01 01:52:26.210827    control     old_page          1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读书数据\n",
    "con=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='kai199418',db='abtest',use_unicode=True, charset=\"utf8\")\n",
    "data=pd.read_sql(\"select * from ab_data\",con=con)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 由于计算的是比例, 所以按照第6题所示的公式 ,从H1得知, 有变大趋势, 所以是单侧右检验\n",
    "# 先求得P0\n",
    "# 由于该数据分流有些问题, 所以需要过滤掉有问题的数据\n",
    "control_p = data.converted[(data.group =='control') & (data.landing_page == 'old_page')].mean()\n",
    "control_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 因为converted 值只有0 和 1 所以求平均和求比例是一样的\n",
    "countall= data.converted[(data.group =='control') & (data.landing_page == 'old_page')].count()\n",
    "count_one = data.converted[(data.group =='control') & (data.landing_page == 'old_page') & (data.converted == 1)].count()\n",
    "count_one / countall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10589344218918344"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算P0(1-P0)  = a1 \n",
    "p0_1_p0 = control_p*(1 - control_p )\n",
    "p0_1_p0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6448536269514722"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算Z1-alpha\n",
    "alpha = 0.05\n",
    "z1_alpha = stats.norm.ppf(1-alpha)\n",
    "z1_alpha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8416212335729143"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算 z1_beta\n",
    "beta = 0.2\n",
    "z1_beta =  stats.norm.ppf(1-beta)\n",
    "z1_beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11338571609917422"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#一类指标: H1:  实验组点击率 > 对照组点击率 \n",
    "treatment_p = control_p + 0.01\n",
    "# p*(1-p)\n",
    "p_1_p = treatment_p * (1-treatment_p)\n",
    "p_1_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6701.938803160929"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本量计算, \n",
    "n1 = p0_1_p0 * ((z1_alpha + z1_beta* np.sqrt(p_1_p/p0_1_p0))/ 0.01)**2\n",
    "n1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算P值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id         145274\n",
       "timestamp       145274\n",
       "group           145274\n",
       "landing_page    145274\n",
       "converted       145274\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取对照组数据\n",
    "control_data = data.loc[(data.group =='control') & (data.landing_page == 'old_page')]\n",
    "control_data.count()\n",
    "control_data.drop_duplicates()\n",
    "control_data.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id         145311\n",
       "timestamp       145311\n",
       "group           145311\n",
       "landing_page    145311\n",
       "converted       145311\n",
       "dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取实验组数据\n",
    "treatment_data = data.loc[(data.group =='treatment') & (data.landing_page == 'new_page')]\n",
    "treatment_data.count()\n",
    "treatment_data.drop_duplicates()\n",
    "treatment_data.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均符合样本量\n",
    "# 统计对照组的点击率\n",
    "control_p = data.converted[(data.group =='control') & (data.landing_page == 'old_page')]\n",
    "\n",
    "control_p.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11880724790277405"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "treatment_p = data.converted[(data.group =='treatment') & (data.landing_page == 'new_page')]\n",
    "treatment_p.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.0015790565976871451"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#alpha =0.05 \n",
    "dif =  treatment_p.mean() - control_p.mean()\n",
    "# 两组点击率之差\n",
    "dif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3254138459199159"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算对照组标准差\n",
    "std = data.converted[(data.group =='control') & (data.landing_page == 'old_page')].std()\n",
    "std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6508276918398318"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "muzhicha = std *2\n",
    "muzhicha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4494012679402463e-06"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "varsum = control_p.var()/control_p.count() + treatment_p.var()/ treatment_p.count()\n",
    "varsum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算dif, 相应的分布概率P\n",
    "p =  stats.norm.cdf(dif,loc= muzhicha,scale= np.sqrt(varsum))\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新banner点击率小于老banner\n"
     ]
    }
   ],
   "source": [
    "# 判断结果\n",
    "if p < alpha:\n",
    "    if dif > 0:\n",
    "        print(\"新banner点击率大于老banner\")\n",
    "    else:\n",
    "        print(\"新banner点击率小于老banner\")\n",
    "else:\n",
    "    print(\"新banner影响老banner阈值\"+ str(muzhicha))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
